package com.example.demo.Controller;
import com.example.demo.Service.myservice;
import com.example.demo.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@RestController
@RequestMapping("/api/upload")
public class FileUploadController {
    @Autowired
    myservice a;
    Result t=new Result();
    // 文件存储的根目录
    private final String uploadDir = "uploads/";  // 文件存储目录，可以自定义
    // 确保文件夹存在
    public FileUploadController() {
        File directory = new File(uploadDir);
        if (!directory.exists()) {
            directory.mkdirs();  // 如果目录不存在则创建
        }
    }
    // 处理文件上传请求

    @PostMapping("/file")
    public void uploadFile(@RequestParam("file") MultipartFile file,
                              @RequestParam("fileName") String fileName,
                              @RequestParam("uri") String uri) throws IOException {
            // 生成保存的目标路径
            Path path = Paths.get(uploadDir + fileName);
            // 将文件保存到指定路径
            Files.copy(file.getInputStream(), path);
        String tem="C:\\Users\\刘昊\\Desktop\\后端\\"+path;
        t=a.test(tem);
        System.out.println("打印结果"+t);
    }
    @GetMapping("/get")
    public Result abc(){
        return t;
    }
}
